/******************************************************************************
 * Filename : uc_ts.h
 * Copyright: 
 * Created  : 2011-2-23 by dfer.li
 * Description -
 * 
 ******************************************************************************/

#ifndef UC_TS_H
#define UC_TS_H

#ifdef __cplusplus
extern "C" {
#endif

#include "uc.h"
#include "uc_utils.h"

#define TS_PKT_SZ 188

#define TS_ZIP_BUF_SIZE 284    // 4 + (188 - 4) * 1.5
#define TS_GROUP_MAX 5000

#define BUF_STAT_FREE ((char)0x0)
#define BUF_STAT_USE ((char)0x1)
#define BUF_STAT_DEPRE ((char)0x2)

// clock tick per ms
#define TS_CLOCK_27KHZ 27000
#define TS_CLOCK_30KHZ 30000

//#define INPUT_BUF_RECV (1024*10)
//#define TS_INPUT_PKT_MAX		300
#define TS_PAT_PID 0x0
#define TS_PMT_TID 0x02

#define STREAM_TYPE_VIDEO_MPEG1     0x01
#define STREAM_TYPE_VIDEO_MPEG2     0x02
#define STREAM_TYPE_AUDIO_MPEG1     0x03
#define STREAM_TYPE_AUDIO_MPEG2     0x04
#define STREAM_TYPE_PRIVATE_SECTION 0x05
#define STREAM_TYPE_PRIVATE_DATA    0x06
#define STREAM_TYPE_AUDIO_AAC       0x0f
#define STREAM_TYPE_AUDIO_AAC_LATM  0x11
#define STREAM_TYPE_VIDEO_MPEG4     0x10
#define STREAM_TYPE_VIDEO_H264      0x1b
#define STREAM_TYPE_VIDEO_VC1       0xea
#define STREAM_TYPE_VIDEO_DIRAC     0xd1

#define STREAM_TYPE_AUDIO_AC3       0x81
#define STREAM_TYPE_AUDIO_DTS       0x8a

#define MEDIA_TYPE_VIDEO 0
#define MEDIA_TYPE_AUDIO 1
#define MEDIA_TYPE_PRIVATE 2

typedef struct group_buf {
	char *parse_buf;
	char *flag_arr;
	int start_ptr;
	int end_ptr;
	int size;
} group_buf_s;

typedef struct group {
	ucnode_s node;	
	char *data;
	int size;
	int pkt_cnt;
} group_s;

typedef struct group_list {
	uclist_s head;
	int gcnt;
} group_list_s;

typedef struct range {
	int range_l;
	int range_r;
} range_s;

typedef struct range_node {
	int range_l;
	int range_r;
	ucnode_s node;
} range_node_s;

typedef struct range_list {
	ucnode_s head;
	int range_cnt;
} range_list_s;

enum elem_type {
	ELEM_TYPE_CNTL,
	ELEM_TYPE_AUDIO,
	ELEM_TYPE_VIDEO
};

enum elem_type_range_cnt {
	ELEM_CNTL_RANGE = 1,
	ELEM_AUDIO_RANGE = 4,
	ELEM_VIDEO_RANGE = 64
};

typedef struct elem {
	ucnode_s node;
	int deprecated;
	int pid;
	int pkt_cnt;
	int end_flag;
	int range_cnt_max;
	int range_ptr;
	range_s range[0];
} elem_s;

typedef struct elem_list {
	uclist_s head;
	group_buf_s buf;
	int vconti_index;
	int aconti_index;
	int elem_cnt;
	int vpid;
	int apid;
} elem_list_s;

typedef struct ts_group {
	elem_list_s elist;
	group_list_s glist;
	uclock_s lock;
} ts_group_s;

typedef struct ts_checker {
	int tsc_vindex;
	int tsc_aindex;
	int tsc_vpid;
	int tsc_apid;
} ts_checker_s;

extern int64_t ucts_get_pcr_ms(char *pkt, int clock_frequence);
extern int64_t ucts_grp_pcr_ms(char *grp_buf, int grp_size, int clock_frequence); 

extern int ucts_get_pid(char *pkt);
extern int ucts_is_start_unit(char *pkt);
extern int ucts_get_continue_index(char *pkt);

extern int ucts_compress(UC_U8 *pkt, UC_U8 *outbuf, int buf_size);
extern int ucts_uncompress(UC_U8 *buf, int buf_size, UC_U8 *pkt);

extern void *ucts_grp_init(int max_pkt_cnt, int v_id, int a_id); /* return ts_grp */
extern void ucts_grp_reset(void *ts_grp);
extern void ucts_grp_release(void *ts_grp);
extern int ucts_grp_input(void *ts_grp, char *buf_in, int size, int drop_flag);
extern int ucts_grp_get_one(void *ts_grp, char **buf_out, int *out_size);
extern void ucts_grp_free(char *buf);
extern char *ucts_grp_pack(char *buf, int size, int *out_size,int data_offset, int vpid);
extern char *ucts_grp_unpack(char *buf, int size, int *pkt_cnt);

extern void *ucts_checker_init(int v_id, int a_id);
extern int ucts_check_continuity(void *checker, char *pkts, int len);
extern int ucts_get_file_pmt_info(char *filename, int *v_pid, int *a_pid, int *v_stream_type, int *a_stream_type);


#ifdef __cplusplus
}
#endif

#endif

